//
//  ClubGameToolsViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/24.
//

import UIKit

class ClubGameToolsViewController: ClubBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("游戏工具").color(.black).font(16.font_semiBold)
        .alignment(.center).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubGameToolsListCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 92.0.Scale
        return tableView
    }()

    private var imgArray = ["Home_gameTools_0","Home_gameTools_1","Home_gameTools_2"]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            titleLabel
            tableView
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.centerHorizontally()
        titleLabel.CenterY == backButton.CenterY
        tableView.leading(0).trailing(0).top(.navi_height).bottom(0)
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
}


extension ClubGameToolsViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return imgArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubGameToolsListCell", for: indexPath) as! ClubGameToolsListCell
        cell.contentImg.image = imgArray[indexPath.row].toImage
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
            
            var urlString = ""
            var title = ""
            if indexPath.row == 0 {
                title = "卡组库"
                urlString = "https://h5-tool.lwan.com/#/homePackages/cardGroup?token=" + uid
            }else if indexPath.row == 1 {
                title = "卡牌图鉴"
                urlString = "https://h5-tool.lwan.com/#/homePackages/cardGuid?token=" + uid
            }else if indexPath.row == 2 {
                title = "卡组模拟"
                urlString = "https://h5-tool.lwan.com/#/pages/cardGroup/index?token=" + uid
            }
            
            urlString = urlString + "&model=ios"
            
            let webView = ClubGameToolsWebViewController()
            webView.urlString = urlString
            webView.titleName = title
            self.navigationController?.pushViewController(webView, animated: true)
        }else{
            self.navigationController?.pushViewController(ClubLoginViewController(), animated: true)
        }
        
    }
}


//MARK:
class ClubGameToolsListCell: UITableViewCell {
    
    let contentImg = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        subviews{
            contentImg
        }
        
        contentImg.leading(12.0.Scale).trailing(12.0.Scale).top(12.0.Scale).height(80.0.Scale)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
